package com.ebt.m.customer.util;

import android.content.Context;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ebt.m.AppContext;
import com.ebt.m.customer.view.RecorderWrapper;
import com.ebt.m.customer.view.TapeMessageContainerView;
import com.ebt.m.customer.view.TapeMessageView;
import com.ebt.m.data.bean.BeanCustomerTape;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rick.Wang on 2016/6/14.
 */
public class TapeViewUtil {

    private static RecordManager mRecordManager;
    private static RecorderWrapper mRecorderWrapper;

    /**
     * 生成包含多个TapeMessageView的LinearLayout
     * @param context
     * @param noteUuid
     * @param callBack TapeMessageView的callBack
     * @param rect 要生成的LinearLayout的大小
     * @return
     */
    public static View genTapeViewsByNoteUuid(Context context, String noteUuid, TapeMessageView.TapeOperate callBack, Rect rect){
        mRecordManager = RecordManager.getInstance().setAppContext((AppContext) (context.getApplicationContext()));
//        mRecorderWrapper = new RecorderWrapper(context.getApplicationContext(), RecorderManager.RECORDER_DIR);
        mRecorderWrapper = RecorderWrapper.getInstance();
        ArrayList<BeanCustomerTape> tapes = getTapeDatas(noteUuid, mRecordManager);
        return genTapeMessageViews(context, tapes, callBack, rect, mRecorderWrapper);
    }

    private static ArrayList<BeanCustomerTape> getTapeDatas(String noteUuid, RecordManager recordManager){
        ArrayList<BeanCustomerTape> tapes = new ArrayList<>();
        tapes.addAll(recordManager.getBeanCustomerTapes(noteUuid));
        return tapes;
    }

    private static LinearLayout genTapeMessageViews(Context context, ArrayList<BeanCustomerTape> tapes,
              TapeMessageView.TapeOperate callBack, Rect rect, final RecorderWrapper recorderWrapper){

        if(tapes == null || tapes.size()==0) return null;
        TapeMessageContainerView ll = new TapeMessageContainerView(context);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
        ll.setLayoutParams(new LinearLayout.LayoutParams(
                (rect == null || rect.width()<=0)?LinearLayout.LayoutParams.WRAP_CONTENT : rect.width(),
                (rect == null || rect.height()<=0)?LinearLayout.LayoutParams.WRAP_CONTENT : rect.height()));

        if(callBack == null){
            callBack = new TapeMessageView.TapeOperate() {
                @Override
                public void onTapeToPlay(TapeMessageView view, BeanCustomerTape tape, float progress) {
                    if(recorderWrapper != null) {
                        boolean isPlaying = false;
                        try {
                            isPlaying = recorderWrapper.isPlaying();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
//                            recorderWrapper = null;
//                            recorderWrapper = RecorderWrapper.getInstance();
                        }
                        if(isPlaying){
                            recorderWrapper.releasePlayer();
                        }
                        recorderWrapper.playRecordFile(tape.getFilePath());
                        recorderWrapper.setPlayerOnCompletionListener(view);
                    }
                }
                @Override
                public void onTapeToPause(TapeMessageView view, BeanCustomerTape tape, float progress) {
                    if(recorderWrapper != null){
                        recorderWrapper.pausePlayRecordFile();
                    }
                }
            };
        }
        ll.setWeightSum(4);
        ll.setTapeMessageViewCallback(callBack);
        int size = tapes.size();
        LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        viewParams.weight = 1;

        for(int i = 0; i < size; i++){
            ll.addView(genTapeMessageView(context, tapes, i, callBack), viewParams);
        }
        return ll;
    }

    public static TapeMessageView genTapeMessageView(Context context,
             List<BeanCustomerTape> tapes, int i, TapeMessageView.TapeOperate callBack){

        TapeMessageView tapeMessageView = new TapeMessageView(context);
        tapeMessageView.setTapeOperate(callBack);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
        int marginH = DisplayUtil.dip2px(context, 2);
        int marginL = 0;
        int marginR = 0;
        if(i != 0) marginL = marginH;
        if(i != (tapes.size()-1)) marginR = marginH;
        params.setMargins(marginL, 0, marginR, 0);
        tapeMessageView.update(tapes.get(i));
        tapeMessageView.setLayoutParams(params);
        return tapeMessageView;
    }
}
